package com.mayblackcat.amoy.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.mayblackcat.common.exception.BizCodeEnum;
import com.mayblackcat.common.vaild.UpdateGroup;
import com.mayblackcat.common.vaild.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.mayblackcat.amoy.product.entity.BrandEntity;
import com.mayblackcat.amoy.product.service.BrandService;
import com.mayblackcat.common.utils.PageUtils;
import com.mayblackcat.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 * 使用OSS云存储服务，为了适应分布式系统。使用服务端签名后直接上传到阿里云存储，减轻服务器压力。
 *
 * @author mayblackcat
 * @email 1923384291@qq.com
 * @date 2021-08-16 10:56:29
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * @Valid:用于校验前端发过来的数据与实体类中的字段进行比对，进行统一校验
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody BrandEntity brand ,BindingResult result){
		if(result.hasErrors()){
		    Map<String,String> map=new HashMap<>();
		    //获取校验的错误结果
            result.getFieldErrors().forEach((fieldError)->{
                //获取字段错误提示
                String message=fieldError.getDefaultMessage();
                //获取错误字段名字
                String field=fieldError.getField();
                map.put(field,message);
            });
          //返回异常信息
          return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(),BizCodeEnum.VALID_EXCEPTION.getMsg()).put("data",map);
        }else {
            brandService.save(brand);

            return R.ok();
        }


    }

    /**
     * 修改
     * @Validated:
     * 1.分组校验，通过接口进行校验分组
     * 2.在没有默认分组的校验注解，在分组校验情况不生效
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 修改状态
     * @param brand
     * @return
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
